package com.yunkeji.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yunkeji.common.core.domain.PageQuery;
import com.yunkeji.common.core.page.TableDataInfo;
import com.yunkeji.common.enums.app.CheckStatus;
import com.yunkeji.common.exception.ServiceException;
import com.yunkeji.common.utils.StringUtils;
import com.yunkeji.dao.domain.Moments;
import com.yunkeji.dao.mapper.MomentsMapper;
import com.yunkeji.dao.vo.MomentsVo;
import com.yunkeji.oss.core.OssClient;
import com.yunkeji.oss.factory.OssFactory;
import com.yunkeji.system.domain.bo.CheckBo;
import com.yunkeji.system.domain.bo.MomentsBo;
import com.yunkeji.system.service.IMomentsService;

import lombok.RequiredArgsConstructor;

import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 动态Service业务层处理
 *
 * @author zhang
 * @date 2024-07-10
 */
@RequiredArgsConstructor
@Service
public class MomentsServiceImpl implements IMomentsService {

    private final MomentsMapper baseMapper;

    @Override
    public void pass(CheckBo bo) {
        Moments moments = baseMapper.selectById(bo.getId());
        if (!moments.getStatus().equals(CheckStatus.待审核.getCode())) {
            throw new ServiceException("操作失败");
        }
        moments.setStatus(CheckStatus.审核通过.getCode());
        baseMapper.updateById(moments);
    }

    @Override
    public void refuse(CheckBo bo) {
        Moments moments = baseMapper.selectById(bo.getId());
        if (!moments.getStatus().equals(CheckStatus.待审核.getCode())) {
            throw new ServiceException("操作失败");
        }
        moments.setStatus(CheckStatus.审核拒绝.getCode());
        baseMapper.updateById(moments);

        OssClient storage = OssFactory.instance();

        List<String> pics = moments.getPics();
        if (CollectionUtil.isNotEmpty(pics)) {
            pics.forEach(
                    item -> {
                        storage.delete(item);
                    });
        }
    }

    /** 查询动态 */
    @Override
    public MomentsVo queryById(Long id) {
        return baseMapper.selectVoById(id);
    }

    /** 查询动态列表 */
    @Override
    public TableDataInfo<MomentsVo> queryPageList(MomentsBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<Moments> lqw = buildQueryWrapper(bo);
        Page<MomentsVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /** 查询动态列表 */
    @Override
    public List<MomentsVo> queryList(MomentsBo bo) {
        LambdaQueryWrapper<Moments> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<Moments> buildQueryWrapper(MomentsBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<Moments> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getUserId() != null, Moments::getUserId, bo.getUserId());
        lqw.eq(StringUtils.isNotBlank(bo.getContent()), Moments::getContent, bo.getContent());
        lqw.eq(StringUtils.isNotBlank(bo.getPics()), Moments::getPics, bo.getPics());
        lqw.eq(bo.getLikeNum() != null, Moments::getLikeNum, bo.getLikeNum());
        lqw.eq(bo.getReplyNum() != null, Moments::getReplyNum, bo.getReplyNum());
        lqw.eq(bo.getCityId() != null, Moments::getCityId, bo.getCityId());
        lqw.eq(bo.getProvinceId() != null, Moments::getProvinceId, bo.getProvinceId());
        lqw.eq(bo.getStatus() != null, Moments::getStatus, bo.getStatus());
        lqw.orderByDesc(Moments::getCreateTime);
        return lqw;
    }

    /** 新增动态 */
    @Override
    public Boolean insertByBo(MomentsBo bo) {
        Moments add = BeanUtil.toBean(bo, Moments.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /** 修改动态 */
    @Override
    public Boolean updateByBo(MomentsBo bo) {
        Moments update = BeanUtil.toBean(bo, Moments.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /** 保存前的数据校验 */
    private void validEntityBeforeSave(Moments entity) {
        // TODO 做一些数据校验,如唯一约束
    }

    /** 批量删除动态 */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            // TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
